package com.sikao.app.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.sikao.app.R;
import com.sikao.app.dao.CheckAppUpdateModel;
import com.sikao.app.net.HttpApi;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.net.UpdateApk;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.ConstantClass;
import com.sikao.app.utils.DataCleanManager;
import com.sikao.app.utils.EquipmentUtils;
import com.sikao.app.utils.JSONUtil;

import java.io.File;

/**
 * 个人设置
 */
public class SettingsActivity extends BaseActivity implements View.OnClickListener {

	private TextView titleTv;
	private ImageView backIv;
	private RelativeLayout clearCache;
	private TextView softwareVersionTv;
	private RelativeLayout update;
	private Button signOutBtn;
	private CheckAppUpdateModel checkAppUpdateModel;
	private RelativeLayout rl1;
	private RelativeLayout rl2;
	private RelativeLayout rl3;
	private RelativeLayout rl4;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_settings);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initData() {
	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		backIv = (ImageView) findViewById(R.id.title_left_img);
		clearCache = (RelativeLayout) findViewById(R.id.clear_cache);
		softwareVersionTv = (TextView) findViewById(R.id.software_version_tv);
		update = (RelativeLayout) findViewById(R.id.update);
		signOutBtn = (Button) findViewById(R.id.sign_out_btn);

		rl1 = (RelativeLayout) findViewById(R.id.rl1);
		rl2 = (RelativeLayout) findViewById(R.id.rl2);
		rl3 = (RelativeLayout) findViewById(R.id.rl3);
		rl4 = (RelativeLayout) findViewById(R.id.rl4);
	}

	@Override
	protected void initListener() {
		backIv.setOnClickListener(this);
		clearCache.setOnClickListener(this);
		update.setOnClickListener(this);
		signOutBtn.setOnClickListener(this);
		rl1.setOnClickListener(this);
		rl2.setOnClickListener(this);
		rl3.setOnClickListener(this);
		rl4.setOnClickListener(this);
	}

	@Override
	protected void init() {
		titleTv.setText("设置");
		backIv.setVisibility(View.VISIBLE);
		getCache();
		softwareVersionTv.setText("v" + EquipmentUtils.getVersionName());
	}

	private void getCache() {
		File externalCacheDir = this.getExternalCacheDir();
		try {
			DataCleanManager.getCacheSize(externalCacheDir);
			String cache = DataCleanManager.getCacheSize(externalCacheDir);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_left_img:
				finish();
				break;
			case R.id.clear_cache:
				clearCache();
				break;
			case R.id.update:
				checkAppUpdate();
				break;
			case R.id.sign_out_btn:
				signOut();
				break;
			case R.id.rl1:
				Intent intent1 = new Intent();
				intent1.setClass(SettingsActivity.this, ChangeLoginPswActivity.class);
				startActivity(intent1);
				break;
			case R.id.rl2:
				Intent intent2 = new Intent();
				intent2.setClass(SettingsActivity.this, ChangePhoneNumberActivity.class);
				startActivity(intent2);
				break;
			case R.id.rl3:
				Intent intent3 = new Intent();
				intent3.setClass(SettingsActivity.this, ChangePayPswActivity.class);
				startActivity(intent3);
				break;
			case R.id.rl4:
				Intent intent4 = new Intent();
				intent4.setClass(SettingsActivity.this, RetrievePayPswActivity.class);
				startActivity(intent4);
				break;
			default:
				break;
		}
	}

	private void clearCache() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("确认清除缓存吗?");
		builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				ImageLoader.getInstance().clearMemoryCache();
				ImageLoader.getInstance().clearDiskCache();
				DataCleanManager.cleanExternalCache(SettingsActivity.this.getApplicationContext());
				DataCleanManager.cleanInternalCache(SettingsActivity.this.getApplicationContext());
				getCache();
				dialog.dismiss();
				Toast.makeText(SettingsActivity.this, "成功清除缓存", Toast.LENGTH_SHORT).show();
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	private void signOut() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("确定退出当前账号吗?");
		builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				ConstantClass.state = 0;
				HttpApi.sessionId = null;
				ConstantClass.type = 0;
				ConstantClass.imgUrl = null;
				ConstantClass.userName = null;
				ConstantClass.phoneNumber = null;
				ConstantClass.balanceValue = 0;
				ConstantClass.frozenFund = 0;
				ConstantClass.creditValue = 0;
				finish();
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	private void checkAppUpdate() {
		new UpdateApk(this, true).checkUpdateInfo(true);
	}

}
